package com.hivekion.system.controller;

import com.hivekion.common.annotation.AutoLog;
import com.hivekion.common.entity.TreeNode;
import com.hivekion.common.enums.OperationTypeEnum;
import com.hivekion.common.exception.BusinessException;
import com.hivekion.system.domain.vo.SysDeptCreateInputVo;
import com.hivekion.system.domain.vo.SysDeptModelVo;
import com.hivekion.system.domain.vo.SysDeptUpdateInputVo;
import com.hivekion.system.domain.vo.SysDeptViewVo;
import com.hivekion.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/dept")
public class SysDeptController {

      @Autowired
      ISysDeptService deptService;

      @GetMapping("/getInfo")
      public SysDeptModelVo getInfo(String deptId) {
	    return deptService.getDeptInfo(deptId);
      }

      @GetMapping("/getTreeSelect")
      public List<TreeNode> getTreeSelect() {
	    return deptService.getTreeSelect();
      }

      @GetMapping("/getList")
      public List<SysDeptViewVo> getList() {
	    return deptService.getList();
      }

      @PostMapping("/add")
      @PreAuthorize("@Permission.hasPermi('pro:sys:org:add')")
      @AutoLog(value = "新增部门", operationType = OperationTypeEnum.INSERT, module = "系统管理/部门管理")
      public boolean add(@RequestBody SysDeptCreateInputVo inputVo) {
	    if (deptService.checkDeptNameUnique(inputVo.getDeptName(), "")) {
		  throw new BusinessException(500, "部门名称重复");
	    }
	    return deptService.create(inputVo);
      }

      @PostMapping("/checkDeptNameUnique")
      public boolean checkDeptNameUnique(String deptName, String deptId) {
	    return deptService.checkDeptNameUnique(deptName, deptId);
      }

      @PutMapping("/edit")
      @PreAuthorize("@Permission.hasPermi('pro:sys:org:edit')")
      @AutoLog(value = "更改部门信息", operationType = OperationTypeEnum.UPDATE, module = "系统管理/部门管理")
      public boolean update(@RequestBody SysDeptUpdateInputVo inputVo) {
	    if (inputVo.getId().equals(inputVo.getParentId())) {
		  throw new BusinessException(500, "禁止选择本部门为父级部门");
	    }
	    if (deptService.checkDeptNameUnique(inputVo.getDeptName(), inputVo.getId())) {
		  throw new BusinessException(500, "部门名称已存在");
	    }
	    return deptService.update(inputVo);
      }

      @DeleteMapping("/remove")
      @PreAuthorize("@Permission.hasPermi('pro:sys:org:remove')")
      @AutoLog(value = "删除部门", operationType = OperationTypeEnum.DELETE, module = "系统管理/部门管理")
      public boolean delete(String depId) {
	    return deptService.delete(depId);
      }

      @PostMapping("/changeStatus")
      @PreAuthorize("@Permission.hasPermi('pro:sys:org:edit')")
      @AutoLog(value = "更改状态", operationType = OperationTypeEnum.UPDATE, module = "系统管理/部门管理")
      public boolean changeStatus(String deptId, Integer status) {
	    return deptService.changeStatus(deptId, status);
      }
}
